home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Sample Code / Snippets / Toolbox / Dialog popups / DialogTest.c next >
Encoding:
C/C++ Source or Header  |  1994-10-28  |  5.1 KB  |  193 lines  |  [TEXT/KAHL]

  1. // uses universal headers
  2. //
  3. // snippet to demonstrate the use of the system 7 popup 
  4. // control cdef in a program using modal dialogs
  5. //
  6. // Nick Thompson, 4/26/94
  7. //
  8. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10. /* 
  11. Sponge ID:105648       New Question
  12.   
  13. The content of your link dated: 31 May 1994 follows:
  14. ******************************************************************************
  15. ------------------------------------------------------------------------------
  16.  
  17. I’m not sure who I should refer this to, but I believe I have found a bug in
  18. the new Dialog Manager routines SetDialogDefaultItem & GetStdFilterProc.
  19.  
  20. What I am trying to do is create a default button that is initially grayed out
  21. and later enabled when the dialog box is filled in.  What is happening is that
  22. my button is getting re-enabled (unless I step into the source debugger).  My
  23. other non default button is getting grayed out as expected.  I tried
  24. rearranging the order that things were done to get it to stay gray, but I
  25. couldn’t.  I finally had to resort to using a filter proc and a global variable
  26. to tell me if I should gray the button after the initial call to
  27. GetStdFilterProc.
  28.   
  29. ******************************************************************************
  30.  
  31. */
  32.  
  33. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit) ;
  34.  
  35. const char kEnter    = 0x03 ;
  36. const char kReturn    = 0x0D ;
  37. const char kEscape    = 0x1B ;
  38. const char kPeriod    = '.' ;
  39.  
  40. const char kThePopupMenu = 4 ;        // popup is the fourth item in the dialog
  41. const char kUserTextArea = 5 ;        // this is a rect for us to write a string
  42.                                     // with the item chosen in it
  43.  
  44.  
  45. // Structure for the private data for a popup control.
  46. // This structure is documented on page 5-77 
  47. // Inside Macintosh: Macintosh Toolbox Essentials
  48.  
  49. typedef struct popupPrivateData {
  50.     MenuHandle     mHandle;     // the popup menu handle 
  51.     short         mID;        // the popup menu ID 
  52.     // after these two public fields is the mPrivate private data, 
  53.     // which may be any old size and should not be messed with 
  54. }    popupPrivateData;
  55.  
  56.  
  57.  
  58.  
  59. main() {
  60.  
  61.     DialogPtr             thePopupDialog ;
  62.     short                itemHit ;
  63.     popupPrivateData    **myPopupPrivateDataPtr ;
  64.     
  65.     short                iKind;
  66.     Handle                iHandle;
  67.     Rect                iRect;
  68.     
  69.     MenuHandle            thePopupMenuHdl ;
  70.     Str255                theItem ;
  71.     
  72.     OSErr                 theErr ;
  73.     
  74.     // initialize the toolbox
  75.     InitGraf(&qd.thePort); InitFonts(); InitWindows(); InitMenus();
  76.     TEInit(); InitDialogs((long)nil); InitCursor(); FlushEvents(everyEvent,0);
  77.     
  78.     thePopupDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  79.  
  80.     SetPort( (GrafPtr)thePopupDialog ) ;
  81.     theErr =  SetDialogDefaultItem(thePopupDialog, ok) ;
  82.  
  83.     if( theErr != noErr )
  84.         ExitToShell() ;
  85.  
  86.     
  87.     do {
  88.     
  89.         ModalDialog ( OurFilter, &itemHit );
  90.         
  91.         if( itemHit == kThePopupMenu ) {
  92.         
  93.             // the user choose the popup.  The item number selected will be the control value
  94.             // we need to get the menuhandle associated with the control, it is in the private
  95.             // control data field, as documented in Inside Macintosh: Toolbox page 5-77
  96.             
  97.             // get the control handle for the popup            
  98.             GetDItem ( thePopupDialog, kThePopupMenu, &iKind, &iHandle, &iRect) ;
  99.             
  100.             // extract from the control the menuhandle
  101.             myPopupPrivateDataPtr = (popupPrivateData **)(**(ControlHandle)iHandle).contrlData ; 
  102.             thePopupMenuHdl = (**myPopupPrivateDataPtr).mHandle ;
  103.     
  104.             // get the string associated with the users selection
  105.             GetItem ( thePopupMenuHdl, GetCtlValue((ControlHandle)iHandle), theItem );
  106.             
  107.             // get the rect we are drawing in
  108.             GetDItem ( thePopupDialog, kUserTextArea, &iKind, &iHandle, &iRect) ;
  109.             SetIText ( iHandle, theItem );
  110.             
  111.             // this ensures that the update handler in the filter proc is called
  112.             // as that is where we enable or disable the OK button
  113.             InvalRect( &iRect ) ;
  114.             
  115.             // reset itemHit to something else or we'll continually redraw
  116.             itemHit = 0 ;
  117.         
  118.         }
  119.         
  120.     } while( itemHit != ok ) ;
  121.     
  122.     DisposDialog ( thePopupDialog );
  123.  
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit)
  131. {
  132.  
  133.     ModalFilterUPP         theProc ;
  134.     Boolean                retVal ;
  135.     
  136.     static    Boolean        isDisabled = false ;
  137.     OSErr                theErr = noErr ;
  138.     
  139.     // stuff for getditems etc
  140.     Str255                theItem ;
  141.     short                iKind;
  142.     Handle                iHandle;
  143.     Rect                iRect;
  144.     
  145.     
  146.     // get the std filter proc
  147.     
  148.     theErr = GetStdFilterProc( &theProc ) ;
  149.     
  150.     if( theErr != noErr )
  151.         ExitToShell() ;
  152.         
  153.     // try to call the standard filter, if it handles the event, we don't
  154.     if( !(retVal = CallModalFilterProc(theProc, dlg, event, itemHit)) )
  155.     {
  156.         switch (event->what) {
  157.     
  158.             case nullEvent:
  159.                 break;
  160.     
  161.             case keyDown:
  162.             case autoKey:
  163.                 retVal = false;
  164.                 break ;
  165.     
  166.             case updateEvt:
  167.                 // get the text item we are drawing in
  168.                 GetDItem ( dlg, kUserTextArea, &iKind, &iHandle, &iRect) ;
  169.                 GetIText ( iHandle, theItem );
  170.                 isDisabled = theItem[0] == '\0' ;
  171.                 
  172.                 // enable or disable the OK button depending on whether 
  173.                 // we made a selection yet
  174.                 GetDItem(  dlg, ok, &iKind, &iHandle, &iRect) ;
  175.                 if( isDisabled )
  176.                     HiliteControl( (ControlHandle)iHandle, 255 ) ;
  177.                 else
  178.                     HiliteControl( (ControlHandle)iHandle, 0 ) ;
  179.                     
  180.                 retVal = false;
  181.                 break ;
  182.     
  183.             default:
  184.                 retVal = false;
  185.                 break ;
  186.         }
  187.     }
  188.     
  189.     return retVal ;
  190. }
  191.  
  192.  
  193.